<template>
	<view class="goods-list">
		<view v-for="(item,i) in goodsResult" :key="i" @click="toDetail(item.goods_id)">
			<goods-item :goods="item"></goods-item>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					// 请求参数对象
					pagenum: 1, //页码
					pagesize: 10, //一页显示多少条
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
				},
				total: 0, //总共数据
				goodsResult: [], //商品数据
				isloading: false //节流阀开关，默认关闭
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || '';
			this.queryObj.cid = options.cid || '';
			this.searchGoodsList();
		},
		onReachBottom() {
			if (this.queryObj.pagenum * this.queryObj.pagesize > this.total) {
				return uni.$showMsg("我是有底线的！！~")
			}
			if (this.isloading) return;
			this.queryObj.pagenum++;
			this.searchGoodsList();
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1;
			this.total = 0;
			this.isloading = false;
			this.goodsResult = [];
			//重新发起请求
			this.searchGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async searchGoodsList(cb) {
				this.isloading = true; //打开节流阀
				const {
					data
				} = await uni.$http.get("/api/public/v1/goods/search", this.queryObj);
				this.isloading = false; //关闭节流阀
				cb && cb();
				if (data.meta.status !== 200) return uni.$showMsg();
				this.goodsResult = [...this.goodsResult, ...data.message.goods];
				this.total = data.message.total;
			},
			toDetail(goods_id) {
				uni.navigateTo({
					url: "/subpkg/detail/detail?goods_id=" + goods_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-list {
		background-color: #fff;
	}
</style>